package com.whj.controller;

import com.whj.entity.SysUser;
import com.whj.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/tologin")
    public String tologin() {
        return "login";
    }
    @RequestMapping("/toregister")
    public String toregister() {
        return "register";
    }
    @RequestMapping("/login")
    public String login(HttpSession  session, Model  model, String userName, String password) {
        SysUser user=userService.selectUser(userName, password);
        if(user!=null){
            session.setAttribute("user",user);
            if(user.getUserType().equals("3")){
                return "Message";
            }
            if(user.getUserType().equals("2")){
                return "redirect:/loan/showLoan";

            }
            System.out.println(password);
            return "/index";

        }
        model.addAttribute("msg","用户名或密码错误");
        return "login";
    }
    @RequestMapping("/register")
    public String register(Model model,SysUser user) {

        System.out.println(user.getUserName());
        if(userService.ishave(user)){
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encode = encoder.encode(user.getPassword());
            user.setPassword(encode);
            userService.insertUser(user);
            model.addAttribute("msg","注册成功,请登录");
            return "/login";
         }
        model.addAttribute("msg","用户名已存在");
            return "register";
    }
    @RequestMapping("/showZhuanJia")
    public String showZhuanJia(Model model) {
        model.addAttribute("zj",userService.showZhuanJia());
        return "projects";
    }
    @RequestMapping("toindex")
    public String toindex() {
        return "index";
    }
}
